// pages/makeSnapshot/makeSnapshot.js
const app=getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode: 'dateTime',
    index:0,
    count: 3,
    latitude: 0, //首次加载维度
    longitude: 0, //首次加载的经度
    eventType:null,//事件类型
    location: "", //定位的位置
    time:0,//时间
    name:"",
    phone:"",
    desc:"",
    nickName:0,
    headImg:0,
    accounts: [ // value传给后台的值 ， label页面上显示的文字
          {value:'environment', label:'环境卫生'},
          {value:'traffic', label:'交通秩序'},
          {value:'advice', label:'建言献策'},
          {value:'progress', label:'乡风文明'},
          {value:'management', label:'圩镇管理'},
        ],
    imgs: [],
  },

   // 下拉切换事件类型
   bindAccountChange(e){
        this.setData({
          eventType: this.data.accounts[e.detail.value].label
        })
  },
  onPickerChange(e) {//返回回调函数
      this.data.time=e.detail.value;
},
  //选择日期
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  //定位
  moveToLocation() {
    let that = this
    wx.chooseLocation({
        success: function (res) {
            //赋值给data中的location
            that.setData({
              location: res.name
            })
        },
        //错误信息
        fail: function () {
            console.log(err);
        }
    })
},
chooseImage: function (e) {
  
  wx.chooseImage({
    sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
    sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
    success: res => {
      
      this.uploadFile(res.tempFilePaths)
    }
  })
},
uploadFile(urls) {
  wx.showLoading({
    title: '正在上传',
  })
  let imgs=[]
  let index=0
  console.log(urls)
  for(let i=0;i<urls.length;i++){
    wx.cloud.uploadFile({
      cloudPath:'upLoadpic/'+'snapshot'+Date.now() + ".jpg",
      filePath: urls[i],
      success: (res)=>{
          index++
          imgs.push(res.fileID)
          wx.showToast({
            title: "上传成功",
            icon: "none",
            duration: 1500
          })
          if(index===urls.length){
            this.setData({
              imgs:this.data.imgs.concat(imgs)
            })
            wx.hideLoading()
          }
      },
      fail: function (err) {
        wx.showToast({
          title: "上传失败",
          icon: "none",
          duration: 2000
        })
      }
    })
  }
        
      
},
// 删除图片
deleteImage: function (e) {
  var that = this
  wx.showModal({
    title: "提示",
    content: "是否删除",
    success: function (res) {
      if (res.confirm) {
        for (var i = 0; i < that.data.imgs.length; i++) {
          if (i == e.currentTarget.dataset.index) that.data.imgs.splice(i, 1)
        }
        that.setData({
          imgs: that.data.imgs
        })
      } else if (res.cancel) {
      }
    }
  })
},
//放大预览图片
handleImagePreview(e) {
  const idx = e.target.dataset.idx
  const imgs = this.data.imgs
  wx.previewImage({
    current: imgs[idx], //当前预览的图片
    urls: imgs, //所有要预览的图片
  })
},
//提交
bindSubmit:function(e){
// this.bindUpload();
for (const key in this.data) {
  if(this.data[key]===""||this.data.imgs.length===0){
    wx.showToast({
      title: '不能有未填项！',
      icon: 'error',
      duration: 4000
    }) 
    return
  }
  if(key==="phone"){
    if(this.data.phone.length<8||(!/^\d+$/.test(parseInt(this.data.phone)))){
      wx.showToast({
        title: '手机格式错误，请重新填写',
        icon: 'error',
        duration: 4000
      })
      return   
    }
  }
}
db.collection('snapShot').add({
  // data 字段表示需新增的 JSON 数据
  data: {
    eventType:this.data.eventType,
    location:this.data.location,
    time:this.data.time,
    name:this.data.name,
    phone:this.data.phone,
    descri:this.data.desc, 
    photoUrl:this.data.imgs,
    reply:'',
    state:0,
    headImg:app.globalData.userInfo.avatarUrl,
    nickname:app.globalData.userInfo.nickName,
  },
  
  success:(res)=> {
    // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
    wx.showToast({
      title: '提交成功！',
      icon: 'success',
      duration: 2000
    })
    setTimeout(()=>{
      wx.navigateBack({})
    },2000)
  }
})
}
})